package org.czl.leetcode4;

public class l1771 {
    public int longestPalindrome(String word1, String word2) {
        char[] c = (word1 + word2).toCharArray();
        int n = c.length;
        int[][] dp = new int[n + 1][n + 1];
        int ans = 0;
        for(int i = n - 1; i >= 0; i--){
            dp[i][i] = 1;
            for(int j = i + 1; j < n; j++){
                if(c[i] == c[j]){
                    dp[i][j] = dp[i + 1][j - 1] + 2;
                    if(i < word1.length() && j >= word1.length()){
                        ans = Math.max(ans, dp[i][j]);
                    }
                }else{
                    dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]);
                }
            }
        }
        return ans;
    }
}
